<template>
    <div>
        <h1>新增车辆</h1>
        <tbody class="table">
        <tbody>
            <tr>
                <td>照片</td>
                <td>
                    <input type="file" @change="upload">
                    <img style="height: 60px;width: 60px;" :src="axios.defaults.baseURL + data.Img">
                </td>
            </tr>
            <tr>
                <td>品牌名</td>
                <td>
                    <input type="text" v-model="data.Cname">
                </td>
            </tr>
            <tr>
                <td>车牌号</td>
                <td>
                    <input type="text" v-model="data.CarNum">
                </td>
            </tr>
            <tr>
                <td>行车证号</td>
                <td>
                    <input type="text" v-model="data.XCZH">
                </td>
            </tr>
            <tr>
                <td>座位</td>
                <td>
                    <input type="text" v-model="data.ZW">
                </td>
            </tr>
            <tr>
                <td>价格</td>
                <td>
                    <input type="text" v-model="data.JG">
                </td>
            </tr>
            <tr>
                <td>购买日期</td>
                <td>
                    <input type="date" v-model="data.date">
                </td>
            </tr>
            <tr>
                <td>备注</td>
                <td>
                    <input type="text" v-model="data.BZ">
                </td>
            </tr>
            <tr>
                <td>颜色</td>
                <td>
                    <input type="text" v-model="data.Colour">
                </td>
            </tr>
             <tr>
                <td>国产/进口</td>
                <td>
                    <input v-model="data.GCJK" type="radio" name="GCJK" value="国产">国产
                    <input v-model="data.GCJK" type="radio" name="GCJK" value="进口">进口
                </td>
            </tr>
            <tr>
                <td>地址</td>
                <td>
                    <input type="text" v-model="data.Address">
                </td>
            </tr>
            <tr>
                <td>手机号</td>
                <td>
                    <input type="text" v-model="data.Phone">
                </td>
            </tr>
            <tr>
                <td>车主姓名</td>
                <td>
                    <input type="text" v-model="data.Uname">
                </td>
            </tr>
            <tr>
                <td>类型</td>
                <td>
                    <select v-model="data.Tid">
                        <option value="0">选择类型</option>
                        <option v-for="d in Bang" :value="d.Tid">{{ d.Tname }}</option>
                    </select>

                </td>
            </tr>
            <tr>
                <td>状态</td>
                <td>
                    <input v-model="data.Stear" type="radio" name="Staer" value="1">正常
                    <input v-model="data.Stear" type="radio" name="Staer" value="2">停用
                    <input v-model="data.Stear" type="radio" name="Staer" value="3">报废
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="button" class="btn btn-success" value="新增" @click="saveinfo">
                    <a href="/">返回会员列表</a>
                </td>
            </tr>
        </tbody>
        </tbody>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios'
import { ref,onMounted} from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
//上传
const upload = (abc: any) => {
    //取文件  创建表单数据对象 把文件保存到表单数据对象
    let f = abc.target.files[0]
    let fd = new FormData();
    fd.append("file", f);

    axios.post("/api/WJSC/UpLoad", fd).then(res => {
        data.value.Img = res.data;
    })
}
onMounted(()=>{
    bang();
})
const bang=()=>{
    axios.get("/api/Car/bang").then(res=>{
        Bang.value=res.data
    })

}
const Bang=ref([{
    "Tid": 0,
    "Tname": "string"
}])
const data = ref({
   "Cid": 0,
  "Cname": "",
  "Img": "",
  "IsDelete": true,
  "Stear": 0,
  "CarNum": "",
  "Phone": "",
  "Colour": "",
  "XCZH": "",
  "date": "2025-08-09T02:00:54.625Z",
  "JG": 0,
  "Uname": "",
  "Address": "",
  "GCJK": "",
  "BZ": "",
  "Tid": 0,
  "ZW":""
})
const saveinfo = () => {
    let zz=/^1[3-9]\d{9}$/;
    if(!zz.test(data.value.Phone))
    {
       alert("手机格式不对")
       return;
    }
    let cc=/^[\u4e00-\u9fa5]{2,20}$/;
    if(!cc.test(data.value.Cname))
    {
       alert("品牌名格式不对")
       return;
    }
    if(data.value.Cname=="")
    {
      alert("品牌名不能为空")
      return;
    }
    if(data.value.ZW=="")
    {
      alert("座位不能为空")
      return;
    }
    if(data.value.CarNum=="")
    {
      alert("品牌号不能为空")
      return;
    }
    if(data.value.Tid==0)
    {
      alert("类别不能为空")
      return;
    }
    if(data.value.Img=="")
    {
      alert("照片不能为空")
      return;
    }
    axios.post('/api/Car/tianjia', data.value).then(res => {
        if (res.data > 0) {
            alert("新增成功")
            router.push("/showcar");
        }
        else {
            alert("新增失败...")
        }
    })
}
</script>

<style scoped></style>